package com.longrise.android.byjk.plugins.course.freecourse;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.CourseDefaultEntityBeanItem;
import com.longrise.android.byjk.model.CourseDefaultItem;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.byjk.plugins.course.coursedetail.CourseDetailActivity2;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.datasource.remote.LoadDataManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by callxkj on 2017/11/23.
 * 免费试看
 */

public class FreeCourseActivity extends BaseActivity2 implements FreeCourseAdapter.OnItemClickListener {


    private RecyclerView mRcv;
    private FreeCourseAdapter mAdapter;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_freecourse;
    }

    @Override
    public void initView() {
        setToolbarTitle("免费试看");
        mRcv = (RecyclerView) findViewById(R.id.freecourse_rcv);
        initRcv();
        initData();
    }

    @Override
    public void onToolbarBackClick() {
        finish();
    }

    private void initRcv() {
        View headView = View.inflate(mContext,R.layout.item_seconddetailfragment_headview,null);
        mAdapter = new FreeCourseAdapter(getDefatutData());
        mAdapter.addHeaderView(headView);
        mRcv.setLayoutManager(new LinearLayoutManager(mContext));
        mRcv.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this);
    }

    @Override
    public void reload() {
        super.reload();
        initData();
    }

    /**
     * 数据还没加载出来的时候 显示的假数据
     */
    private List<CourseDefaultEntityBeanItem> getDefatutData() {
        List<CourseDefaultEntityBeanItem> data = new ArrayList<>();
        for (int x = 0; x < 10; x++) {
            CourseDefaultEntityBeanItem item = new CourseDefaultEntityBeanItem(CourseDefaultItem.TYPE1);
            data.add(item);
        }
        return data;
    }


    private void initData() {
        EntityBean bean = new EntityBean();
        bean.set("pagesize", "1000");
        bean.set("pagenum", "1");
        bean.set("cardno", UserInfor.getInstance().getUsersfzh());
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl,
                "course_sfreeCourses", fbean, new LoadDataManager.OnRequestCallListener() {

                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        try {
                            EntityBean bean = (EntityBean) result;
                            int value = (Integer) bean.get("resultstate");
                            String resultdesc = bean.getString("resultdesc");
                            if (value == 1) {
                                EntityBean resultBean = bean.getBean("result");
                                EntityBean[] results = resultBean.getBeans("result");
                                parseData(results);
                            } else {
                                parseData(null);
                                showToast(resultdesc);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            parseData(null);
                        }
                    }

                    @Override
                    public void onFinished(String key, String service) {

                    }

                    @Override
                    public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                        showNonNetPage();
                    }
                });
    }

    /**
     * 解析数据
     */
    private void parseData(EntityBean[] results){
        List<CourseDefaultEntityBeanItem> items = new ArrayList<>();
        if(results !=null && results.length >0){
            for (EntityBean bean : results){
                CourseDefaultEntityBeanItem item = new CourseDefaultEntityBeanItem(CourseDefaultEntityBeanItem.TYPE2);
                item.setEntityBean(bean);
                items.add(item);
            }
        }
        mAdapter.refreshData(items);
    }


    /**
     * item 点击
     */
    @Override
    public void onClick(EntityBean bean) {
        try {
            if (bean != null) {
                String courseid = bean.getString("courseid");
                String realprice = bean.getString("realprice");
                Bundle bundle = new Bundle();
                bundle.putString(CourseDetailActivity2.INTENT_EXTRA_PARAMS, courseid);
                bundle.putString(CourseDetailActivity2.COURSE_PRICE, realprice);
                startActivity(CourseDetailActivity2.class, bundle);
            }
        } catch (Exception e) {

        }
    }
}
